package com.pro.back.service.impl;

import com.pro.back.bean.Classes;
import com.pro.back.bean.ClassesExample;
import com.pro.back.bean.common.ApiRes;
import com.pro.back.dao.ClassesMapper;
import com.pro.back.service.ClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: jingwenjuan
 * @Description:
 * @Date: 2025-04-19
 */
@Service
public class ClassesServiceImpl implements ClassesService {

    @Autowired
    private ClassesMapper classesMapper;

    /**
     * 查看所有的班级信息
     * @return
     */
    @Override
    public ApiRes selectAll() {
        List<Classes> classes = classesMapper.selectByExample(null);
        return new ApiRes(classes);
    }

    /**
     * 新增或编辑班级信息
     * @param classes
     * @return
     */
    @Override
    public ApiRes inserClasses(Classes classes) {
        ClassesExample example = new ClassesExample();

        if (classes.getId()!=null) {//编辑
            example.createCriteria().andNameEqualTo(classes.getName()).andIdNotEqualTo(classes.getId());
            List<Classes> users = classesMapper.selectByExample(example);
            if (users!=null && users.size()>0) {
                return new ApiRes(500,null,"班级名不能重复，请重试!");
            }
            classesMapper.updateByPrimaryKeySelective(classes);
        } else {
            example.createCriteria().andNameEqualTo(classes.getName());
            List<Classes> users = classesMapper.selectByExample(example);
            if (users!=null && users.size()>0) {
                return new ApiRes(500,null,"班级名不能重复，请重试!");
            }
            classesMapper.insertSelective(classes);
        }

        return new ApiRes("success");
    }

    @Override
    public ApiRes delData(Long id) {
        ClassesExample ex = new ClassesExample();
        ex.createCriteria().andIdEqualTo(id);
        classesMapper.deleteByPrimaryKey(id);
        return new ApiRes("success");
    }
}
